<?php
include_once('config/config.php');

$LoginAdmin = auth();

if ($_POST) {
    // 获取ajax的操作
    $action = $_POST['action'];

    if ($action == 'del') {
        $id = $_POST['id'];

        $Sql = "SELECT * FROM `stu_admin` WHERE `id` = '$id'";

        $admin = find($Sql);

        if (!$admin) {
            error('删除的管理员不存在');
        }

        if ($LoginAdmin['id'] == $id) {
            error('这是当前登录的账号，无法删除');
        }

        // 删除函数 返回ajax请求的数据
        $result = delete('admin', "`id` = '$id'");

        if ($result) {
            if (!empty($admin['avatar'])) {
                @is_file($admin['avatar']) && @unlink($admin['avatar']);
            }

            success('删除该管理员成功');
        } else {
            error('删除该管理员失败');
        }
    }
}

// 当前页
$page = !empty($_GET['page']) ? $_GET['page'] : 1;

// 获取需要搜索的用户名
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

// 搜索的条件
$where = 1;

if (!empty($keyword)) {
    $where = "`username` LIKE '%$keyword%'";
}

// 每页显示多少条数据
$limit = 2;

// 显示多少个页码
$size = 3;

// 查询admin数据表的数据总数
$countSql = "SELECT COUNT(*) AS c FROM `stu_admin` WHERE $where";

$Count = find($countSql);

// 调用分页函数
$ShowPage = page($page, $Count['c'], $limit, $size);

// 偏移量 -> 索引值
$start = ($page - 1) * $limit;

// 查询数据
$AdminSql = "SELECT * FROM `stu_admin` WHERE $where ORDER BY `id` DESC LIMIT $start,$limit";

$AdminData = all($AdminSql);

/* 
    列表页
        删除按钮 => 在首页删除功能
    新增页
    修改页
    查看页 => 列表页无法把数据的所有的字段显示时需要查看页

*/

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->

<body>
    <!--<![endif]-->

    <!-- 引用头部 -->
    <?php include_once('common/header.php') ?>

    <!-- 引用菜单 -->
    <?php include_once('common/nav.php') ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">管理员列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">AdminList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminadd.php'"><i class="icon-plus"></i>新增管理员</button>
                    <form method="get" style="display:flex;align-items:center;margin-top:10px;">
                        <input style="margin: 0; margin-right:10px;" type="text" name="keyword" placeholder="<?php echo $keyword ? $keyword : '请输入用户名' ?>">
                        <input type="submit" class="btn btn-primary" value="搜索">
                    </form>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>用户名</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($AdminData as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td>
                                        <a href="<?php echo $item['avatar'] ? $item['avatar'] : 'assets/images/person.png' ?>" target="_blank">
                                            <img src="<?php echo $item['avatar'] ? $item['avatar'] : 'assets/images/person.png'; ?>" width="40" alt="">
                                        </a>
                                    </td>
                                    <td><?php echo $item['username']; ?></td>
                                    <td>
                                        <a href="adminedit.php?id=<?php echo $item['id'] ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-id="<?php echo $item['id'] ?>" class="del" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <!-- <div class="pagination">
                    <ul>
                        <li><a href="#">Prev</a></li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">Next</a></li>
                    </ul>
                </div> -->

                <?php echo $ShowPage; ?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确认该管理员？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" id="del" data-del-id data-dismiss="modal">删除</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>

<?php include_once('common/script.php'); ?>

<script>
    /* 
        1、data-id 获取并且传到弹出窗里
        2、点击删除执行ajax
    */
    $('.del').click(function() {
        // 点击哪个就获取哪个id
        var id = $(this).data('id')
        // 赋值到删除按钮
        $('#del').data('del-id', id)
    })

    // 删除按钮
    $('#del').click(function() {
        // 获取需要删除的id
        var id = $(this).data('del-id')

        $.ajax({
            url: 'adminlist.php',
            type: 'post',
            data: {
                // $_POST['id']
                // 'id':id
                id,
                'action': 'del'
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 1) {
                    alert(res.msg)

                    location.reload()
                } else {
                    alert(res.msg)
                }
            }
        })
    })
</script>